package com.lemon.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.lemon.domain.vo.PcdVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lemon.mapper.PcdMapper;
import com.lemon.domain.Pcd;
import com.lemon.service.PcdService;

/**
 * 省市区Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-08-03
 */
@Service
public class PcdServiceImpl extends ServiceImpl<PcdMapper,Pcd> implements PcdService
{
    @Autowired
    private PcdMapper pcdMapper;

    @Override
    public List<PcdVo> getAll() {
        List<PcdVo> all = pcdMapper.getAll();
        List<PcdVo> fu = all.stream().filter(s -> s.getParentId().equals(0)).toList();
        fu.forEach(s->setZiList(s,all));
        return fu;
    }

    @Override
    public List<PcdVo> getCityList() {
        List<PcdVo> all = pcdMapper.getAll();
        List<PcdVo> fu = all.stream().filter(s -> s.getParentId().equals(0)).toList();
        List<PcdVo> list = new ArrayList<>();
        fu.forEach(s->{
            List<PcdVo> cityList = all.stream().filter(a -> a.getParentId().equals(s.getValue())).toList();
            list.addAll(cityList);
        });
        return list;
    }


    private void setZiList(PcdVo fu,List<PcdVo> all){
        List<PcdVo> ziList = all.stream().filter(s -> s.getParentId().equals(fu.getValue())).toList();
        fu.setChildren(ziList);
        ziList.forEach(s->setZiList(s,all));
    }
}
